package lancooPaperContent;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;

import org.apache.commons.lang3.StringEscapeUtils;

import lancooUpLoad.PaperInfor;
import testPaperWrong.WrongQuesPaperActivity;

import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperFavoritesPage.FavoritesPagePaperActivity.InforFavoritesPage;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;
import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;
import static testPaperWrongHistory.HistoryWrongQuesPaperActivity.InforHistoryWrongQues;


/**
 * Created by Administrator on 2017/4/5.
 */
public class ContentYdjdAdapter extends WrapBaseAdapter

{

    private LayoutInflater mInflater;
    private Context mContext;
    //当前试题数量
    private int i_ItemSum;
    //当前试题在当前试卷的ID
    private int PaperID;
    private int ModelFrom;//试卷来源

    private String itemContent = "";//获取【阅读简答】大题题干
    private String typeName;//题目类型
    private double correctRate;//准确率
    private  int state;//作答状态
    private double score;//小题总分

    private ViewHolder holder = null;
    public class ViewHolder{
        TextView TvTypeName;
        Button BtnIntoStudy;
        //        TextView TvTheme;//大题文本题干1
        WebView WvTheme;
        public ViewHolder(View convertView) {
            TvTypeName = (TextView) convertView.findViewById(R.id.tv_type_name);
            BtnIntoStudy = (Button) convertView.findViewById(R.id.btn_into_study);
//            TvTheme = (TextView) convertView.findViewById(R.id.tv_theme);
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
        }
    }

    public ContentYdjdAdapter(Context context, int itemSum, int paperID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.i_ItemSum = itemSum;
        this.PaperID = paperID;
        this.ModelFrom = modelFrom;
    }

    @Override
    public int getCount() {
        return i_ItemSum;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_content_ydjd_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
        * 获取阅读简答题目题干信息
        * */
        getContent();

        return convertView;
    }


    /*
    * 获取题目题干信息
    * */
//    Handler htmlHandlerContent = new Handler();
    private void getContent(){

        if(ModelFrom == PaperDef.FROM_BASE_REVIEW) {
            state = InforBasicReview.getState();
            typeName = InforBasicReview.getPaperContent().get(PaperID).getTypeName();
            correctRate = InforBasicReview.getPaperContent().get(PaperID).getCorrectRate();
            itemContent = InforBasicReview.getPaperContent().get(PaperID).getTheme();
            score = InforBasicReview.getPaperContent().get(PaperID).getScore();
        }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
            state = InforDifficultReview.getState();
            typeName = InforDifficultReview.getTypeName();
            correctRate = InforDifficultReview.getCorrectRate();
            itemContent = InforDifficultReview.getTheme();
            score = InforDifficultReview.getScore();
        }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            state = InforGuessTesting.getState();
            typeName = InforGuessTesting.getPaperContent().get(PaperID).getTypeName();
            correctRate = InforGuessTesting.getPaperContent().get(PaperID).getCorrectRate();
            itemContent = InforGuessTesting.getPaperContent().get(PaperID).getTheme();
            score = InforGuessTesting.getPaperContent().get(PaperID).getScore();
        }else if(ModelFrom== PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
            state = InforWrongQues.get(PaperID).getState();
            typeName = InforWrongQues.get(PaperID).getTypeName();
            correctRate = InforWrongQues.get(PaperID).getCorrectRate();
            itemContent = InforWrongQues.get(PaperID).getTheme();
            score = InforWrongQues.get(PaperID).getScore();
        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG_History||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG_History){
            state = InforHistoryWrongQues.get(PaperID).getState();
            typeName = InforHistoryWrongQues.get(PaperID).getTypeName();
            correctRate = InforHistoryWrongQues.get(PaperID).getCorrectRate();
            itemContent = InforHistoryWrongQues.get(PaperID).getTheme();
            score = InforHistoryWrongQues.get(PaperID).getScore();
        }else if(ModelFrom==PaperDef.FROM_FAVOURITE_REVIEW) {
            state = InforFavoritesPage.getState();
            typeName = InforFavoritesPage.getTypeName();
            correctRate = InforFavoritesPage.getCorrectRate();
            itemContent = InforFavoritesPage.getTheme();
            score = InforFavoritesPage.getScore();
        }


//        strItemContent = StringEscapeUtils.unescapeXml(strItemContent);
//        final String finalStrItemContent = strItemContent;
//        new Thread(new Runnable()
//        {
//            @Override
//            public void run()
//            {
//                final Spanned text = Html.fromHtml(finalStrItemContent, Control.imgGetter,null);
//                htmlHandlerContent.post(new Runnable()
//                {
//                    @Override
//                    public void run()
//                    {
//                        Tv.setText(text);
//                    }
//                });
//            }
//        }).start();

        if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            holder.TvTypeName.setText("题目类型-"+typeName+" ("+score+"分)");
        }else {
            holder.TvTypeName.setText("题目类型-"+typeName);
        }

        if(ModelFrom == PaperDef.FROM_BASE_REVIEW||ModelFrom == PaperDef.FROM_DIFFERENT_REVIEW) {
            if(state == 2) {
                if (correctRate < 0.7) {
                    holder.BtnIntoStudy.setVisibility(View.VISIBLE);
                } else {
                    holder.BtnIntoStudy.setVisibility(View.GONE);
                }
            }else{
                holder.BtnIntoStudy.setVisibility(View.GONE);
            }
        }else{
            holder.BtnIntoStudy.setVisibility(View.GONE);
        }
        holder.BtnIntoStudy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ModelFrom==PaperDef.FROM_BASE_REVIEW) {
                    PaperInfor.QuesID = InforBasicReview.getPaperContent().get(PaperID).getQuesID();
                    WrongQuesPaperActivity.launch(null,mContext, PaperInfor.QuesID, PaperInfor.QuesFrom,PaperDef.FROM_BASE_REVIEW_WRONG);
                }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
                    PaperInfor.QuesBrief = InforDifficultReview.getQuesID();
                    WrongQuesPaperActivity.launch(null,mContext, PaperInfor.QuesID, PaperInfor.QuesFrom,PaperDef.FROM_DIFFERENT_REVIEW_WRONG);
                }
            }
        });

        itemContent = StringEscapeUtils.unescapeXml(itemContent);
        holder.WvTheme.loadDataWithBaseURL("about:blank",itemContent,"text/html","utf-8",null);
        holder.WvTheme.getSettings().setDefaultFontSize(18);
        holder.WvTheme.setFocusable(false);
    }


}